package com.grapevine.contact;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
import com.grapevine.contact.adapter.RecentAdapter;
import com.grapevine.contact.config.ConfigKey;
import com.grapevine.contact.config.SharePrefareManager;
import com.grapevine.contact.util.AndroidPhoneLocationLoader;

public class CallLogActivity extends ListActivity implements OnScrollListener
{
	private static final int SCAN_DIALOG = 0;

	private static final int ALL_MODE = 0;
	private static final int MISS_MODE = 1;
	private static final int INCOMING_MODE = 2;
	private static final int OUT_COMING_MODE = 3;

	private AndroidPhoneLocationLoader mAndroidPhoneLocationLoader;

	private int mCurrentMode;
	// listView.setDivider(new ColorDrawable(color))
	private Cursor cursor;
	private RecentAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recent_activity);
		mAndroidPhoneLocationLoader = new AndroidPhoneLocationLoader(this);
		cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,
				CallLog.Calls.DEFAULT_SORT_ORDER);
		startManagingCursor(cursor);
		adapter = new RecentAdapter(this, R.layout.recent_item, cursor, mAndroidPhoneLocationLoader);
		setListAdapter(adapter);
		mCurrentMode = ALL_MODE;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);
		Cursor cursor = (Cursor) adapter.getItem(position);
		long nId = cursor.getLong(cursor.getColumnIndex(CallLog.Calls._ID));
		Intent intent = new Intent(this, DetialActivity.class);
		intent.putExtra(ModeManager.MODE, ModeManager.HISTORY_RECORD);
		intent.putExtra(DetialActivity.ID, nId);
		startActivity(intent);
	}

	@Override
	protected void onResume()
	{
		// cursor.requery();
		super.onResume();
		adapter.notifyDataSetChanged();
		Editor editor = SharePrefareManager.getEditor(this);
		editor.putInt(ConfigKey.CURRENT_ACTIVITY, 1);
		editor.commit();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		if (!isHaveLog())
		{
			menu.setGroupVisible(0, false);
		}
		else
		{
			menu.setGroupVisible(0, true);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{

		menu.add(0, Menu.FIRST, 0, "清除通话记录").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		SubMenu subMenu = menu.addSubMenu(2, Menu.FIRST + 1, 0, "查看记录").setIcon(R.drawable.ic_menu_account_list);
		subMenu.add(1, Menu.FIRST + 2, 0, "全部记录").setChecked(true);
		subMenu.add(1, Menu.FIRST + 3, 0, "未接来电");
		subMenu.add(1, Menu.FIRST + 4, 0, "已接电话");
		subMenu.add(1, Menu.FIRST + 5, 0, "已拨电话");
		subMenu.setGroupCheckable(1, true, true);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{

		boolean ret = true;

		switch (item.getItemId())
		{
		case Menu.FIRST:// 清除通话记录
			if (adapter.getCount() > 0)
			{
				getContentResolver().delete(CallLog.Calls.CONTENT_URI, null, null);
				cursor.requery();
				adapter.notifyDataSetChanged();
			}
			else
			{
				return super.onOptionsItemSelected(item);
			}
			return ret;
		case Menu.FIRST + 2:// 查看全部记录
			if (mCurrentMode != ALL_MODE)
			{
				mCurrentMode = ALL_MODE;
				item.setChecked(true);
				cursor.close();
				cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,
						CallLog.Calls.DEFAULT_SORT_ORDER);
			}
			else
			{
				return super.onOptionsItemSelected(item);
			}
			break;
		case Menu.FIRST + 3:// 未接来电
			if (mCurrentMode != MISS_MODE)
			{
				mCurrentMode = MISS_MODE;
				item.setChecked(true);
				cursor.close();
				cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
						CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE, null, CallLog.Calls.DEFAULT_SORT_ORDER);
			}
			else
			{
				return super.onOptionsItemSelected(item);
			}
			break;

		case Menu.FIRST + 4:// 已接电话
			if (mCurrentMode != INCOMING_MODE)
			{
				mCurrentMode = INCOMING_MODE;
				item.setChecked(true);
				cursor.close();
				cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
						CallLog.Calls.TYPE + "=" + CallLog.Calls.INCOMING_TYPE, null, CallLog.Calls.DEFAULT_SORT_ORDER);
			}
			else
			{
				return super.onOptionsItemSelected(item);
			}
			break;
		case Menu.FIRST + 5:// 已拨电话
			if (mCurrentMode != OUT_COMING_MODE)
			{
				mCurrentMode = OUT_COMING_MODE;
				item.setChecked(true);
				cursor.close();
				cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
						CallLog.Calls.TYPE + "=" + CallLog.Calls.OUTGOING_TYPE, null, CallLog.Calls.DEFAULT_SORT_ORDER);
			}
			else
			{
				return super.onOptionsItemSelected(item);
			}
			break;

		default:
			return super.onOptionsItemSelected(item);
		}
		startManagingCursor(cursor);
		adapter = new RecentAdapter(this, R.layout.recent_item, cursor, mAndroidPhoneLocationLoader);
		setListAdapter(adapter);
		return ret;
	}

	@Override
	protected void onDestroy()
	{
		mAndroidPhoneLocationLoader.stop();
		super.onDestroy();
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		switch (id)
		{
		case SCAN_DIALOG:

			break;

		default:
			dialog = super.onCreateDialog(id);
			break;
		}

		return dialog;
	}

	/**
	 * 查看是否还有通话记录
	 * 
	 * @return 还有记录返回 true 其他返回false
	 */
	private boolean isHaveLog()
	{
		boolean ret = false;
		Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[] { CallLog.Calls._ID }, null,
				null, CallLog.Calls.DEFAULT_SORT_ORDER);
		ret = cursor.moveToFirst();
		cursor.close();
		return ret;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
	{
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState)
	{
		if (scrollState == OnScrollListener.SCROLL_STATE_FLING)
		{
			mAndroidPhoneLocationLoader.pause();
		}
		else
		{
			mAndroidPhoneLocationLoader.resume();
		}
	}

}